//
//  XHF_menuView.m
//  menuView
//
//  Created by 徐宏飞 on 16/4/13.
//  Copyright © 2016年 徐宏飞. All rights reserved.
//

#import "XHF_menuView.h"
#import "XHF_MenuTableViewCell.h"
#import "XHF_ShopsCollectionViewCell.h"
#import "XHF_Header_CRView.h"

@interface XHF_menuView ()<UIScrollViewDelegate,
                           UITableViewDataSource,
                           UITableViewDelegate,
                           UICollectionViewDataSource,
                           UICollectionViewDelegate
                          >


@property (weak, nonatomic) IBOutlet UITableView *menuTableView;
@property (weak, nonatomic) IBOutlet UICollectionView *shopsCollectionView;
@property (nonatomic, retain) UIView *indicateView;//选中指示条
@property (nonatomic, assign, getter = isMenuSelected) BOOL menuSelected;//选中状态
@end


@implementation XHF_menuView


- (void)awakeFromNib{
    //配置菜单区按钮
    [self configureMenuTableView];
    //配置商品区按钮
    [self configureShopsCollectionView];
}

#pragma mark - 菜单
static NSString *cellIdentifier = @"cell";
- (void)configureMenuTableView{
   
    [self.menuTableView registerNib:[UINib nibWithNibName:@"XHF_MenuTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellIdentifier];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    
    [self.menuTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
    self.menuSelected = YES;
}

#pragma mark - tableViewDelegate

#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XHF_MenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    //指示条
    self.indicateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 50)];
    self.indicateView.backgroundColor = COLOR_SELECTED;
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    [cell.selectedBackgroundView addSubview:self.indicateView];

    
    return cell;
}

#pragma mark - 选中执行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.menuSelected = YES;
    
    //商品滚动到中心
    [self.shopsCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:indexPath.row]atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
    
    //菜单滚动到中心
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}






#pragma mark - 商品
static NSString *itemIdentifier = @"items";
static NSString *headerIdentifier = @"header";
- (void)configureShopsCollectionView{
    
    //获取宽度
    CGFloat scr_wid = [UIScreen mainScreen].bounds.size.width;
    CGFloat menu_wid = 95;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //item 比例4:5
    CGSize size = CGSizeMake(96, 120);
    
    //4  4s  5  5s
    if (SCREEN_WIDTH == 320) {
        size = CGSizeMake(64, 80);
    }
    
    //6 6s
    if (SCREEN_WIDTH == 375) {
        size = CGSizeMake(80, 100);
    }
    
    
    
    flowLayout.itemSize = size;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    flowLayout.minimumLineSpacing = 8;
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.headerReferenceSize = CGSizeMake(scr_wid - menu_wid, 60);
    
    self.shopsCollectionView.collectionViewLayout = flowLayout;
    
    
    //header reg
    [self.shopsCollectionView registerNib:[UINib nibWithNibName:@"XHF_Header_CRView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];
    
    //item reg
    [self.shopsCollectionView registerNib:[UINib nibWithNibName:@"XHF_ShopsCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:itemIdentifier];

}

#pragma mark - collectionViewDelegate
#pragma mark - 组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 60;
}

#pragma mark - 行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 20;
}

#pragma mark - item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    XHF_ShopsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:itemIdentifier forIndexPath:indexPath];

    return cell;
    
}

#pragma mark - 头部视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"sec%ld", indexPath.section);
    
    XHF_Header_CRView *headerCRView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerIdentifier forIndexPath:indexPath];

    return headerCRView;
}



#pragma mark - scrollViewDelegate

#pragma mark - 菜单取消选中
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.menuSelected = NO;
}

#pragma mark - scroll
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if ([scrollView isEqual:self.shopsCollectionView]) {
        // 这里的60填section的总个数(menu个数)
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:60];
        // 遍历所有可见cel
        for (NSIndexPath *index in [self.shopsCollectionView indexPathsForVisibleItems]) {
            if (indexPath.section > index.section) {
                indexPath = index;
            }
        }
        if (!self.menuSelected) {
            [self.menuTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
        }
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击push到下一页%ld, %ld", (long)indexPath.section, (long)indexPath.row);
}


@end
















